<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/2/22
 * Time: 下午4:26
 */
namespace Home\Controller;
use Common\Controller\HomeBaseController;

class MemberController extends HomeBaseController
{
    /**
     *会员中心首页
     */
    public function index()
    {
        $tmp = session(C('FRONT_SESSION'));
        $user = M('Member')->Field('id,account,nick,reg_time,last_login_time')->where(['id' => $tmp['id']])->find();
        if(!$user){
            $this->error('用户信息查询失败');
        }
        $this->assign('user',$user);
        $this->assign('title','FD商城--会员中心');
        $this->display();
    }

    /**
     *会员的个人信息
     */
    public function member()
    {
        $tmp = session(C('FRONT_SESSION'));
        $user = M('Member_info')->join('left join fd_member on fd_member.id ='.$tmp['id'])->where(['mid' => $tmp['id']])->find();
        if(!$user){
            $this->error('用户信息查询失败');
        }
        $this->assign('user',$user);
        $this->assign('title','FD商城--用户信息');
        $this->display();
    }
    public  function message()
    {
        $this->assign('title','FD商城--我的留言');
        $this->display();
    }


    /**
     *修改会员的的个人信息
     */
    public function edit()
    {
        $tmp = I('post.');
        $id = I('post.id');
        if(!$id){
            $this->error('非法操作');
        }
        print_r($tmp);
        if(empty($tmp)){
            $this->error('非法操作');
        }
        $act = I('post.act');
        if($act == 'act_edit_profile' ){
            //只修改用户的个人信息，不修改密码
            $name = I('post.name');
            $tel = I('post.tel');
            $sex = I('post.sex');
            $d = I('post.birthdayDay');
            $m  = I('post.birthdayMonth');
            $y = I('post.birthdayYear');
            $birthday = $y.'-'.$m.'-'.$d;
            $t = time($birthday);
            $date = [
                'name' => $name,
                'tel'  => $tel,
                'sex'  => $sex,
                'birthday' => $t
            ];
            $in = M('Member_info')->where(['id' => $id])->find();
            if(!$in){
                $this->error('该用户不存在');
            }
            $info = M('Member_info')->where(['mid' => $id])->save($date);
            if(!$info){
                $this->error('修改失败');
            }
                $this->success('修改成功','Member/member');
        }
        if($act == 'act_edit_password'){
            //修改密码
            $old = I('post.old_password');
            $new = I('post.new_password');
            $comfirm = I('post.comfirm_password');
            if(empty($old)){
                $this->error('原密码不能为空');
            }
            if(empty($new)){
                $this->error('新密码不能为空');
            }
            if(empty($comfirm)){
                $this->error('确认密码不能为空');
            }
            $yz = M('Member')->field('password,salt')->where(['id' => $id])->find();
            if(!$yz){
                $this->error('该用户不存在');
            }
            if(md5($old.$yz['salt']) != $yz['password']){
                $this->error('原密码不正确');
            }

            if($new != $comfirm){
                $this->error('两次输入密码不相同');
            }

            $pw = md5($new.$yz['salt']);
            $edit = M('Member')->where(['id'=>$id])->save(['password'=>$pw]);
            if(!$edit){
                $this->error('密码修改失败');
            }
            $this->error('密码修改成功');
        }

    }
}